package com.right.health.app.ui.registered;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewStub;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.magic.basic.activity.BaseActivity;
import com.magic.basic.widget.CircleImageView;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.DoctorBean;
import com.right.health.app.bean.DoctorDetailResponse;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.hyphenate.ui.ChatActivity;
import com.right.health.app.net.DoctorApi;
import com.right.health.app.ui.registered.adapter.DoctorDetailServiceAdapter;
import com.right.health.app.ui.registered.adapter.DoctorDetailTimeAdapter;
import com.right.health.app.utils.GlideUtil;
import com.right.health.app.widget.GridViewEx;
import com.right.health.app.widget.ListViewEx;
import com.vendor.social.utils.CollectionUtil;
import com.vendor.social.utils.ToastUtil;

/*
 * Created by hopeliao on 2019-10-01
 *
 */
public class DoctorDetailActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private static final int DOCTOR_DETAIL_CODE =0 ;

    public static final int DOCTOR_OPERATE_CODE = 1;

    public static final String BUNDLE_DOCTOR_ID_KEY ="doctorId";
    public static final String BUNDLE_SEND_BROADCAST_KEY ="sendBroadcast";

    private CircleImageView mHeadImage;

    private ViewStub mUnfollowViewStub;

    private ViewStub mFollowViewStub;

    private LinearLayout mUnfollowLayout;

    private LinearLayout mFollowLayout;

    private DoctorApi mDoctorApi;

    private String mDoctorId;

    private TextView mNameTv,mFansTv,mDepartmentTv, mUnfollowTv, mIntroduceTv,mFollowTv,mConsultTv;

    private DoctorDetailServiceAdapter mServiceAdapter;

    private DoctorDetailTimeAdapter mTimeAdapter, mOutpatientTimeAdapter;

    private DoctorDetailResponse mDetailResponse;

    private boolean mSendBroadcast;

    public static void launch(Context context,String doctorId,boolean sendBroadcast) {
        Intent intent = new Intent(context, DoctorDetailActivity.class);
        if (!TextUtils.isEmpty(doctorId)) {
            Bundle bundle = new Bundle();
            bundle.putString(BUNDLE_DOCTOR_ID_KEY,doctorId);
            bundle.putBoolean(BUNDLE_SEND_BROADCAST_KEY,sendBroadcast);
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.doctor_detail);
    }

    @Override
    public void findView() {
        mHeadImage = findViewById(R.id.iv_avatar);
        mUnfollowViewStub = (ViewStub) findViewById(R.id.dont_follow_viewStub);
        mFollowViewStub = findViewById(R.id.follow_viewStub);
        mNameTv = findViewById(R.id.tv_name);
        mDepartmentTv = findViewById(R.id.tv_department);
        mFansTv = findViewById(R.id.tv_fans_num);
        mIntroduceTv = findViewById(R.id.tv_introduce);
        mUnfollowTv = findViewById(R.id.tv_follow_cancel);
        mUnfollowTv.setOnClickListener(this);
    }

    @Override
    public void initialize() {
        mDoctorApi = new DoctorApi();
        mDoctorApi.setListener(this);
        Bundle bundle=getIntent().getExtras();
        if (bundle != null&&bundle.containsKey(BUNDLE_DOCTOR_ID_KEY)) {
            mDoctorApi.setTag(mDoctorId);
            mDoctorId = bundle.getString(BUNDLE_DOCTOR_ID_KEY);
            mSendBroadcast = bundle.getBoolean(BUNDLE_SEND_BROADCAST_KEY, false);
            mDoctorApi.addRequestCode(DOCTOR_DETAIL_CODE);
            mDoctorApi.getDoctorDetail(mDoctorId);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_follow:
                mDoctorApi.addRequestCode(DOCTOR_OPERATE_CODE);
                mDoctorApi.followDoctor(mDoctorId);
                break;
            case R.id.tv_follow_cancel:
               mDoctorApi.addRequestCode(DOCTOR_OPERATE_CODE);
               mDoctorApi.cancelFollowDoctor(mDoctorId);
                break;
            case R.id.iv_content:
            case R.id.tv_consult:
                    ChatActivity.launch(this,"u"+mDetailResponse.getDoctor().getUserid());
                break;
        }
    }

    private void showFollowView() {
        mUnfollowViewStub.setVisibility(View.GONE);
        if (mFollowLayout == null) {
           mFollowLayout = (LinearLayout)mFollowViewStub.inflate();
        }else {
            mFollowLayout.setVisibility(View.VISIBLE);
        }
        GridViewEx gridViewEx = (GridViewEx) mFollowLayout.findViewById(R.id.service_grid);
        mServiceAdapter = new DoctorDetailServiceAdapter(this);
        gridViewEx.setAdapter(mServiceAdapter);
        mServiceAdapter.setDataSource(mDetailResponse.getHealthservice());
        mServiceAdapter.setOnClickListener(this);
        mConsultTv = mFollowLayout.findViewById(R.id.tv_consult);
        mConsultTv.setOnClickListener(this);
        if (CollectionUtil.isEmpty(mDetailResponse.getChattime())) {
            mFollowLayout.findViewById(R.id.tv_empty_consult).setVisibility(View.VISIBLE);
        }else {
            ListViewEx timeListView = (ListViewEx) findViewById(R.id.time_list);
            mTimeAdapter = new DoctorDetailTimeAdapter(this);
            timeListView.setAdapter(mTimeAdapter);
            mTimeAdapter.setDataSource(mDetailResponse.getChattime());
            mFollowLayout.findViewById(R.id.tv_empty_consult).setVisibility(View.GONE);
        }
        if (CollectionUtil.isEmpty(mDetailResponse.getViewtime())) {
            mFollowLayout.findViewById(R.id.tv_empty_outpatient).setVisibility(View.VISIBLE);
        }else {
            ListViewEx outpatientTimeList = (ListViewEx) findViewById(R.id.outpatient_time_list);
            mOutpatientTimeAdapter = new DoctorDetailTimeAdapter(this);
            outpatientTimeList.setAdapter(mOutpatientTimeAdapter);
            mOutpatientTimeAdapter.setDataSource(mDetailResponse.getViewtime());
            mFollowLayout.findViewById(R.id.tv_empty_outpatient).setVisibility(View.GONE);
        }
    }

    private void refreshUI() {
        DoctorBean doctor = mDetailResponse.getDoctor();
        mNameTv.setText(doctor.getDoctorname());
        mFansTv.setText(getString(R.string.format_fans_number, doctor.getFans()));
        mIntroduceTv.setText(doctor.getDoctordesc());
        mDepartmentTv.setText(String.format("%s | %s", "", ""));
        if (!TextUtils.isEmpty(doctor.getImageurl())) {
            Glide.with(this).load(doctor.getImageurl()).apply(GlideUtil.getAvatarPlaceholder()).into(mHeadImage);
        }
        if (doctor.getIsfocus()==0) {
            mUnfollowTv.setVisibility(View.GONE);
            showUnfollowView();
        } else if (doctor.getIsfocus() == 1) {
            mUnfollowTv.setVisibility(View.VISIBLE);
            showFollowView();
        }
    }

    private void showUnfollowView() {
        mFollowViewStub.setVisibility(View.GONE);
        if (mUnfollowLayout == null) {
            mUnfollowLayout = (LinearLayout) mUnfollowViewStub.inflate();
            mFollowTv = mUnfollowLayout.findViewById(R.id.tv_follow);
            mFollowTv.setOnClickListener(this);
        }else {
            mUnfollowLayout.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        }else {
            Log.i("TAG", "onResponse: "+response.data);
            if (request.requestCode == DOCTOR_DETAIL_CODE) {
                mDetailResponse=response.cast(DoctorDetailResponse.class);
                if (mDetailResponse != null) {
                    refreshUI();
                }
            } else if (request.requestCode == DOCTOR_OPERATE_CODE) {
                mDoctorApi.addRequestCode(DOCTOR_DETAIL_CODE);
                mDoctorApi.getDoctorDetail(mDoctorId);
                if (mSendBroadcast){
                    sendBroadcast(BroadCastCode.REFRESH_MY_DOCTOR_LIST);
                }
            }
        }
    }
}
